package com.my_test.webmagic.webmagic_modules;

import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.digest.DigestUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.scheduler.RedisScheduler;

/**
 * @author zhangyalin@act-telecom.com
 * @date 2019-06-13 10:37
 */

public class SearchRedisScheduler extends RedisScheduler {

    protected JedisPool pool;

    private static final String QUEUE_PREFIX = "search_queue_";

    private static final String SET_PREFIX = "search_set_";

    private static final String ITEM_PREFIX = "search_item_";

    public SearchRedisScheduler(String host) {
        super(host);
        String s=QUEUE_PREFIX;
    }

    public SearchRedisScheduler(JedisPool pool) {
        super(pool);
        this.pool = pool;
        setDuplicateRemover(this);
    }

    @Override
    protected void pushWhenNoDuplicate(Request request, Task task) {
        Jedis jedis = pool.getResource();
        try {
            jedis.rpush(getQueueKey(task), request.getUrl());
            if (request.getExtras() != null) {
                String field = DigestUtils.shaHex(request.getUrl());
                String value = JSON.toJSONString(request);
                jedis.hset((ITEM_PREFIX + task.getUUID()), field, value);
            }
        } finally {
            pool.returnResource(jedis);
        }
    }

    @Override
    public synchronized Request poll(Task task) {
        Jedis jedis = pool.getResource();
        try {
            String url = jedis.lpop(getQueueKey(task));
            if (url == null) {
                return null;
            }
            String key = ITEM_PREFIX + task.getUUID();
            String field = DigestUtils.shaHex(url);
            byte[] bytes = jedis.hget(key.getBytes(), field.getBytes());
            if (bytes != null) {
                Request o = JSON.parseObject(new String(bytes), Request.class);
                return o;
            }
            Request request = new Request(url);
            return request;
        } finally {
            pool.returnResource(jedis);
        }
    }

    @Override
    protected String getSetKey(Task task) {
        return SET_PREFIX + task.getUUID();
    }

    @Override
    protected String getQueueKey(Task task) {
        return QUEUE_PREFIX + task.getUUID();
    }

    @Override
    protected String getItemKey(Task task)
    {
        return ITEM_PREFIX + task.getUUID();
    }
}
